package com.tsmti.core.interceptor;

import com.alibaba.fastjson.JSON;
import com.tsmti.core.common.annotation.SameUrlData;
import com.tsmti.core.common.model.AjaxJson;
import com.tsmti.core.mapper.JsonMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @Package：com.tsmti.core.annotation
 * @Class：SysLog
 * @Description：拦截器中验证是否重复提交
 *              1、在需要生成token的controller上增加@SameUrlData
 * @Author：zcw
 * @Date：Created in 2018/11/12 10:08
 * @Company:
 * @Version：
 * @Modified By:
 */
public class SameUrlDataInterceptor extends HandlerInterceptorAdapter {

    private static Log log = LogFactory.getLog(SameUrlDataInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method = handlerMethod.getMethod();
            SameUrlData annotation = method.getAnnotation(SameUrlData.class);
            if (annotation != null) {
                //如果重复相同数据
                if(repeatDataValidator(request)){
                    response.setContentType("application/json; charset=UTF-8");
                    AjaxJson success = AjaxJson.success("");
                    response.getWriter().write(JSON.toJSONString(success));
                    response.setStatus(200);
                    return false;
                } else{
                    return true;
                }

            }
            return true;
        } else {
            return super.preHandle(request, response, handler);
        }
    }
    /**
     * 验证同一个url数据是否相同提交  ,相同返回true
     * @param httpServletRequest
     * @return
     */
    public boolean repeatDataValidator(HttpServletRequest httpServletRequest)
    {
        String params = JsonMapper.toJsonString(httpServletRequest.getParameterMap());
        String url=httpServletRequest.getRequestURI();
        Map<String,String> map=new HashMap<String,String>(16);
        map.put(url, params);
        String nowUrlParams=map.toString();

        Object preUrlParams=httpServletRequest.getSession().getAttribute("repeatData");
        //如果上一个数据为null,表示还没有访问页面
        if(preUrlParams==null)
        {
            httpServletRequest.getSession().setAttribute("repeatData", nowUrlParams);
            return false;
        }
        else//否则，已经访问过页面
        {
            //如果上次url+数据和本次url+数据相同，则表示重复添加数据
            if(preUrlParams.toString().equals(nowUrlParams))
            {
                return true;
            }
            else
            {
                //如果上次 url+数据 和本次url加数据不同，则不是重复提交
                httpServletRequest.getSession().setAttribute("repeatData", nowUrlParams);
                return false;
            }

        }
    }

}
